package org.linlinjava.litemall.admin.controller;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.linlinjava.litemall.admin.beans.annotation.LogAnno;
import org.linlinjava.litemall.admin.service.AdminLumiereSubscribeUserService;
import org.linlinjava.litemall.core.util.ResponseUtil;
import org.linlinjava.litemall.core.validator.Order;
import org.linlinjava.litemall.core.validator.Sort;
import org.linlinjava.litemall.db.domain.*;
import org.linlinjava.litemall.db.dto.LumiereSubscribeUserDto;
import org.linlinjava.litemall.db.service.LitemallGoodsService;
import org.linlinjava.litemall.db.service.LitemallShopService;
import org.linlinjava.litemall.db.service.LitemallUserService;
import org.linlinjava.litemall.db.service.LumiereSubscribeService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * @author yliyun
 * @date 2022/09/14
 */
@RestController
@RequestMapping("/admin/lumiere/subscribeUser")
@Validated
public class AdminLumiereSubscribeUserController {

    /**
     * 管理卢米埃订阅用户服务
     */
    @Autowired
    private AdminLumiereSubscribeUserService adminLumiereSubscribeUserService;

    /**
     * 卢米埃订阅服务
     */
    @Autowired
    private LumiereSubscribeService lumiereSubscribeService;

    /**
     * litemall店服务
     */
    @Autowired
    private LitemallShopService litemallShopService;

    /**
     * litemall商品服务
     */
    @Autowired
    private LitemallGoodsService litemallGoodsService;

    /**
     * litemall用户服务
     */
    @Autowired
    private LitemallUserService litemallUserService;

    /**
     * 列表
     *
     * @param userName      用户名
     * @param subscribeName 订阅名字
     * @param status        状态
     * @param page          页面
     * @param limit         限制
     * @param sort          排序
     * @param order         订单
     * @return {@link Object}
     */
    @GetMapping("/list")
    @LogAnno
    public Object list(String userName, String subscribeName, Integer status,
                       @RequestParam(defaultValue = "1") Integer page,
                       @RequestParam(defaultValue = "10") Integer limit,
                       @Sort @RequestParam(defaultValue = "add_time") String sort,
                       @Order @RequestParam(defaultValue = "asc") String order) {

        //缺少订阅名称，订阅方式 从订阅表中查询,
        // 订阅商品，配送门店，   从商品表和门店表查询
        IPage<LumiereSubscribeUser> list =  adminLumiereSubscribeUserService.queryUserList(userName,status , page, limit, sort, order);

        List userDtos = new ArrayList<>();
         list.getRecords().stream().forEach(user -> {
            //通过subscribeId查询订阅名称
            LumiereSubscribeUserDto userDto = new LumiereSubscribeUserDto();
            LumiereSubscribe lumiereSubscribe = lumiereSubscribeService.findById(user.getSubscribeId());

            if (subscribeName == null || subscribeName == "") {
                BeanUtils.copyProperties(user, userDto);
                userDto.setSubscribeName(lumiereSubscribe != null ? lumiereSubscribe.getName() : null);

                //通过shopIds查询门店
                Integer shopId = user.getShopId();
                LitemallShop shop = null;
                if (shopId != null) {
                    shop = litemallShopService.getInfoById(shopId);
                }
                userDto.setShopName(shop != null ? shop.getName() : null);

                //根据goodsId查询商品名称
               if(user.getGoodsId() != null){
                   LitemallGoods good = litemallGoodsService.findById(user.getGoodsId());
                   userDto.setGoodName(good != null ? good.getName() : null);
               }

               if(userDto.getUserName() == null){
                   if(user.getUserId() != null){
                      LitemallUser u =  litemallUserService.findById(user.getUserId());
                      if(ObjectUtil.isNotNull(u)){
                          userDto.setUserName(u.getUsername());
                      }
                   }
               }

                userDtos.add(userDto);
            } else {
                if(lumiereSubscribe.getName().indexOf(subscribeName) != -1) {
                    BeanUtils.copyProperties(user, userDto);
                    userDto.setSubscribeName(lumiereSubscribe != null ? lumiereSubscribe.getName() : null);

                    //通过shopIds查询门店
                    Integer shopId = user.getShopId();
                    LitemallShop shop = null;
                    if (shopId != null) {
                        shop = litemallShopService.getInfoById(shopId);
                    }
                    userDto.setShopName(shop != null ? shop.getName() : null);

                    //根据goodsId查询商品名称
                   if(user.getGoodsId() != null){
                       LitemallGoods good = litemallGoodsService.findById(user.getGoodsId());
                       userDto.setGoodName(good != null ? good.getName() : null);
                   }

                    if(userDto.getUserName() == null){
                        if(user.getUserId() != null){
                            LitemallUser u =  litemallUserService.findById(user.getUserId());
                            userDto.setUserName(u.getUsername());
                        }
                    }
                    userDtos.add(userDto);
                }
            }
        });
        list.setRecords(userDtos);
        return ResponseUtil.okPage(list);
    }
}
